<?php 
//I.F. Nuevo: 2011-02-28; ramarin
require_once('../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/report/moosisca/managecourseslib.php');
require_once($CFG->dirroot . '/course/lib.php');

$systemcontext = get_context_instance(CONTEXT_SYSTEM);

if (!has_capability('report/moosisca:unapprovedcourses', $systemcontext)) {
	error('You do not have the required permission to request approbation of courses.');
}

global $USER;

admin_externalpage_setup('moosisca_unapprovedcourses');
admin_externalpage_print_header();

echo '<form name="courseevaluationrequestform" id="courseevaluationrequestform" method="post" action="courseevaluationrequest.php">';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'"/>';

print_my_courses();

echo '</form>';

admin_externalpage_print_footer();
  
function print_my_courses() {
  global $CFG, $USER;
  
  $courses = get_pending_approvation_courses('mw.id, c.fullname, mw.state, mw.courseid, (
            SELECT mh.timecreated
            FROM mdl_moosisca_workflow_history mh
            WHERE mw.id = mh.workflowid AND mw.state = mh.state
            ORDER BY timecreated DESC
            LIMIT 1
        ) AS timecreated', "mw.state='evaluation'", 'timecreated', 'ASC');
  
  if($courses) {  
    print_heading(get_string('pendingApprovedCourses', 'report_moosisca'));
  
    $table->head = array (get_string("course"), get_string('requestdate', 'report_moosisca'), get_string("action"));
    $table->align = array ("left", "left", "center");
    $table->width = "80%";
    
    $title_approve = get_string('approveUnapproveCourse', 'report_moosisca');
    foreach($courses as $course){      
      $viewbutton = "<a href='" . $CFG->wwwroot . '/admin/report/moosisca/courseapprovation.php?id=' . $course["id"] . "'>" . 
      "<img src='" . $CFG->wwwroot.'/' . $CFG->admin."/report/moosisca/imgs/approve.png' alt='" . $title_approve . "' title='" . $title_approve . "'/></a>";

      $table->data[] = array ("<a href='" . $CFG->wwwroot . '/course/view.php?id=' . $course["courseid"] . "'>" . format_string($course["fullname"]) . "</a>",
                userdate($course['timecreated'], '%Y/%m/%d'), $viewbutton);
    }
    
    print_table($table);
  }
  else {
    print_heading(get_string('noPendingApprovedCoursesFound', 'report_moosisca'));
  }
}
?>